/**
 * Created by Administrator on 2017/3/30.
 * @description 简化modal
 */
const modal = weex.requireModule('customModal');

/**
 * @description toast简化封装
 * @param message
 * @param duration
 */
const toast = function (message, duration) {
    if (!message) {
        return;
    }
    modal.toast({
        message: message,
        duration: duration || 1
    });
};


/**
 * @description alert简化封装,采用链式调用方法
 * @param options
 * @return {*}
 */
const alert = function (options) {
    options = options || {};
    if (typeof options.message === 'undefined') {
        return false;
    }
    options.okTitle = options.okTitle || '确定';

    return new Promise((resove, reject) => {
        modal.alert({
            message: options.message,
            okTitle: options.okTitle
        }, result => {
            if(result === options.okTitle) resove();
            else reject();
        });
    });
};


/**
 * @description confirm 简化, 采用链式写法
 * @param options
 * @return {*}
 */
const confirm = function (options) {
    options = options || {};
    if(typeof options.message === 'undefined'){
        return false;
    }

    options.okTitle = options.okTitle || '确定';
    options.cancelTitle = options.cancelTitle || '取消';

    return new Promise((resove, reject) => {
        modal.warning({
            message: options.message,
            okTitle: options.okTitle,
            cancelTitle: options.cancelTitle
        }, result => {
            if(result === options.okTitle) resove();
            else if(result === options.cancelTitle) reject();
        });
    });
};

export default {
    toast, alert, confirm
};